package view;

import java.awt.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import javax.swing.*;

import controller.EditorController;

import model.*;

/**
 * EditorPanel provides a graphical user interface for setting the tool and
 * various setting of a LevelEditor object.
 * 
 * @author Alexander Craig, Alex Dinardo
 */
@SuppressWarnings("serial")
public class EditorPanel extends JPanel implements EditorListener, PropertyChangeListener{
	
	/** the specific light grey colour of the panel backdrop */
	private int WINDOW_COLOUR = 15724527;
	/** the instance of LevelEditor to listen on */
	private LevelEditor editor;
	/** the label used to show the currently selected tool */
	private JLabel curTool;
	/** the text area used to show the description and instructions of the currently selected tool */
	private JTextArea infoTool;
	/** the JPanel that stores the various components for the tools that have special options */ 
	private JPanel toolOptions;
	
	/**
	 * Generates a new EditorPanel
	 * @param editor	the editor to listen on
	 * @param control	the controller to send button events to
	 */
	public EditorPanel(LevelEditor editor, EditorController control) {
		this.editor = editor;
		editor.addEditorListener(this);
	
		Dimension panelSize = new Dimension(160, 0);
		Dimension otherSize = new Dimension(130, 25);
		this.setPreferredSize(panelSize);
		
		toolOptions = new JPanel();
		toolOptions.setBorder(BorderFactory.createTitledBorder("Tool Options"));
		JComboBox spawnBox = new JComboBox();
		spawnBox.setToolTipText("Spawn Type");
		spawnBox.setActionCommand(EditorTool.SET_SPAWN_TYPE.toString());
		spawnBox.setPreferredSize(otherSize);
		for(GameEntity gameEnt : GameEntity.values()) {
			spawnBox.addItem(gameEnt.toString());
		}
		spawnBox.addActionListener(control);
		spawnBox.setName("setSpawnType");
		toolOptions.add(spawnBox);
		toolOptions.setVisible(false);
		this.add(toolOptions);
		
		JPanel setDotsPanel = new JPanel();
		JFormattedTextField passDots = new JFormattedTextField();//NumberFormat.getNumberInstance());
		JLabel passDotsLabel = new JLabel("Dots:");
		passDotsLabel.setLabelFor(passDots);
		passDotsLabel.setDisplayedMnemonic('d');
		passDots.setPreferredSize(new Dimension(90, 25));
		passDots.setValue(0);//editor.getMaze().getPassDots());
		passDots.addPropertyChangeListener("value", this);
		setDotsPanel.add(passDotsLabel);
		setDotsPanel.add(passDots);
		setDotsPanel.setName("setDotsPanel");
		toolOptions.add(setDotsPanel);
		
		JPanel toolInfo = new JPanel();
		toolInfo.setBorder(BorderFactory.createTitledBorder("Current Tool"));
		this.add(toolInfo);
		curTool = new JLabel("None");
		curTool.setPreferredSize(otherSize);
		toolInfo.add(curTool);
		this.add(toolInfo);
		
		JPanel descriptionPanel = new JPanel();
		descriptionPanel.setBorder(BorderFactory.createTitledBorder("Tool Description"));
		infoTool = new JTextArea();
		infoTool.setPreferredSize(new Dimension(130, 220));
		infoTool.setFont(new Font(Font.SERIF,Font.PLAIN, 12));
		infoTool.setEditable(false);
		infoTool.setLineWrap(true);
		infoTool.setWrapStyleWord(true);
		infoTool.setBackground(new Color(WINDOW_COLOUR));
		descriptionPanel.add(infoTool);
		this.add(descriptionPanel);
	}
	
	@Override
	public void toolChange(EditorEvent event) {
		curTool.setText(editor.getTool().toString());
		infoTool.setText(editor.getTool().getDescription());
		switch(event.getSource().getTool()){
			case ADD_SPAWN: toolOptions.setVisible(true);
							for (Component component : toolOptions.getComponents()){
								if(component.getName() == "setSpawnType"){
									component.setVisible(true);
								}else{
									component.setVisible(false);
								}
							}break;
			case SET_PASS_DOTS: toolOptions.setVisible(true);
								for (Component component : toolOptions.getComponents()){
									if(component.getName() == "setDotsPanel"){
										component.setVisible(true);
									}else{
										component.setVisible(false);
									}
								}break;
			default: toolOptions.setVisible(false);
		}
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent e){
		Object source = e.getSource();
		if (source instanceof JFormattedTextField){
			JFormattedTextField dots = (JFormattedTextField) source;
			Integer i = (Integer) dots.getValue();
			if(i <= editor.getMaze().dotsRemaining())
				editor.setPassDots(i);
		}
		
		
	}
	@Override
	public void mazeChange(EditorEvent event) {}
	@Override
	public void newLevel(EditorEvent event) {}
	@Override
	public void selectionChange(EditorEvent event) {}
}
